<?php //strip
/**
 * Administration table implementation for a single reservation.
 * 
 * @package odReservationsWpPlugin
 * @subpackage odReservationsWpPlugin_Tables
 * @author Ondrej Donek <ondrejd@gmail.com>
 * 
 * @todo Add `cancelled` bulk action.
 * @todo  Add filter by `cancelled`.
 */
class odReservationsWpPlugin_Tables_Reservation extends odWpPlugin_Admin_Table
{
  /**
   * Holds selected member.
   * 
   * @var array
   */
  protected $mMember = null;
  
  /**
   * Constructor. Member can be passed in and than will be shown only 
   * reservations made by him.
   * 
   * @param array|null $aMember
   * @return void
   */
	public function __construct($aMember = null)
	{
    $this->mMember = $aMember;
    
		$adminUrl = get_bloginfo('home') . '/wp-admin/admin.php';
		$model = new odReservationsWpPlugin_Models_Reservation();
		
		parent::__construct(array(
			'formUrl' => $adminUrl . '?page=' . odReservationsWpPlugin::ID . '-my_reservations',
			'editUrl' => $adminUrl . '?page=' . odReservationsWpPlugin::ID . '-reservation',
      'useModel' => true,
			'model' => $model,
			'columns' => array(
				'ID' => array(
					'primary' => true,
					'label' => __(' ', odReservationsWpPlugin::TEXTDOMAIN),
					'valueCol' => 'ID'
				),
				'member_name' => array(
					'label' => __('Člen', odReservationsWpPlugin::TEXTDOMAIN),
					'valueCol' => array('member_num', 'member_name'),
					'valueSep' => ' - ',
					'hidden' => true
				),
        'start' => array(
          'label' => __('Datum a čas zahájení', odReservationsWpPlugin::TEXTDOMAIN),
					'valueCol' => 'start',
					'control' => true
        ),
				'playground_name' => array(
					'label' => __('Hřiště', odReservationsWpPlugin::TEXTDOMAIN),
					'valueCol' => 'playground_name'
				),
				'playmates' => array(
					'label' => __('Počet spoluhráčů', odReservationsWpPlugin::TEXTDOMAIN),
					'valueCol' => 'playmates'
				)
			),
			'bulkActions' => false,
			'filters' => false,
			'maxrows' => 10,
			'primaryCol' => 'ID',
			'tree' => false,
			'editLink' => false,
      'deleteLink' => true
		));
	}
	
  /**
   * Returns reservation that matches given filter.
   * 
   * @param string $aFilter
   * @return odWpReservationPlugin_Models_Reservation_Item
   */
	protected function getItemsCount($aFilter = '1')
	{
    return (!is_array($this->mMember)) 
        ? $this->mModel->getCount('1 ') 
        : $this->mModel->getCount('`t1`.`member_ID` = ' . $this->mMember['ID'] . ' ');
	}
	
  /**
   * Returns reservations that matches given filter.
   * 
   * @param string $aFilter
   * @param integer $aPerPage
   * @param integer $aLimitFrom
   * @return array Array of instances of odWpReservationPlugin_Models_Reservation_Item
   */
	protected function getItems($aFilter = '1', $aPerPage = 10, $aLimitFrom = 0)
	{
    $data = (!is_array($this->mMember)) 
        ? $this->mModel->select('1 ', $aPerPage, $aLimitFrom) 
        : $this->mModel->select('`t1`.`member_ID` = ' . $this->mMember['ID'] . ' ', $aPerPage, $aLimitFrom);
    
    foreach($data as $datarow_id => $datarow) 
      $data[$datarow_id] = new odReservationsWpPlugin_Models_Reservation_Item($datarow);
    
    return $data;
	}
  
} // End of odReservationsWpPlugin_Tables_Reservation
